<?php
class DB {
	private $host = "localhost";
	private $port = "3306";
	private $username = "root";
	private $password = "";
	private $charset = "urf8";
	private $db = "";
	private $conn;

	// 构造函数
	public function __construct($config = array())
	{

		$this->host = $config['host'] ? $config['host'] : $this->host;
		$this->port = $config['port'] ? $config['port'] : $this->port;
		$this->username = $config['username'] ? $config['username'] : $this->username;
		$this->password = $config['password'] ? $config['password'] : $this->password;
		$this->charset = isset($arr['charset']) ? $arr['charset'] : $this->charset;
		$this->db = $config['db'];

		$this->db_connet();
		$this->set_db_charset();
		$this->use_db();
	}

	// 建立连接
	private function db_connet() {
		$this->conn = mysqli_connect($this->host . ':' . $this->port, $this->username, $this->password);
		if ($this->conn->connect_error) {
			die("error:" . $this->conn->connect_error);
		}
	}

	// 设置编码
	private function set_db_charset() {
		mysqli_query($this->conn, "set names {$this->charset}");
	}

	// 选择数据库
	private function use_db() {
		if ($this->db == "") {
			die("Please set db info!");
		}
		mysqli_query($this->conn, "use {$this->db}");
	}

	// 私有的克隆方法，为了防止在类外通过clone生成另一个对象
	private function __clone() {
		die("clone is not allowed");
	}

	// 关闭连接
	public function close() {
		$this->conn->close();
	}

	// 执行一个sql语句
	private function query($sql) {
		$res = mysqli_query($this->conn, $sql);
		if (!$res) {
			$errno = mysqli_errno($this->conn);
			$error = mysqli_error($this->conn);
			echo "Error($errno):$error";
		}
		return $res;
	}

	// 获取条件
	private function get_condition($where) {
		if (is_array($where)) {
			foreach ($where as $key => $value) {
				// 如果是字符串的话需要拼接冒号
				if (is_string($value))
					$condition = $key . "='" . $value . "'";
				else
					$condition = $key . "=" . $value;
			}
		} else {
			$condition = $where;
		}
		return $condition;
	}

	// 获取一条记录
	public function select_one($table, $where) {
		$condition = $this->get_condition($where);
		$sql = "select * from $table where $condition";
		// echo $sql;
		// exit;
		$res = $this->query($sql);
		$row = mysqli_fetch_assoc($res);
		mysqli_free_result($res);
		return $row;
	}

	// 获取所有符合条件的结果集
	public function select($table, $where = null) {
		if ($where == null) {
			$sql = "select * from $table";
		} else {
			$condition = $this->get_condition($where);
			$sql = "select * from $table where $condition";
		}
		$res = $this->query($sql);
		$arr = array();
		while ($row = $res->fetch_row()) {
			$arr[] = $row;
		}
		mysqli_free_result($res);
		return $arr;
	}

	// 插入一条信息
	public function insert($table, $data) {
		$key_str = "";
		$val_str = "";

		foreach ($data as $key => $value) {
			$key_str .= $key . ',';
			$val_str .= "'$value',";
		}

		// 去除字符串两边的逗号
		$key_str = trim($key_str, ',');
		$val_str = trim($val_str, ',');
		$sql = "insert into $table ($key_str) values ($val_str)";
		$res = $this->query($sql);
		return $res;
	}

	// 更新一条记录
	public function update($table, $data, $where) {
		$condition = $this->get_condition($where);
		$str = "";
		foreach ($data as $key => $value) {
			$str .= "$key='$value',";
		}
		$str = trim($str, ',');
		$sql = "update $table set $str where $condition";
		$res = $this->query($sql);
		return $res; 
	}


}

?>